/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Data;
using Core.Model.Events;
using Core.Model.Actions;
using Core.Model.Actions.Elementary;

namespace Core.Model.Ingredients.Links
{
    public class Link : Ingredient
    {
        #region Exchanged Attributes
        private LinkCondition linkCondition;

        public LinkCondition LinkCondition
        {
            get { return linkCondition; }
            set { linkCondition = value; }
        }
        private Core.Model.Actions.Action linkEffect = new Core.Model.Actions.Action();

        public Core.Model.Actions.Action LinkEffect
        {
            get { return linkEffect; }
            set { linkEffect = value; }
        }

        public override void InittInternalAttributes ()
        {
            base.InittInternalAttributes();
        }
        #endregion

        #region Internal Behaviours + Actions
        public override void Activate ()
        {
            if (!RunningStatus )
            {
                base.Activate();

                Engine.MHEGEngine.Instance.AddLink( this );

                RunningStatus = true;
                Engine.MHEGEngine.Instance.Handle( new IsRunning( this ) );
            }
        }

        public override void Deactivate ()
        {
            if ( RunningStatus )
            {
                Engine.MHEGEngine.Instance.RemoveLink( this );
                base.Deactivate();
            }
            
        }
        #endregion


        #region Implementation Specific

        
        public bool ConditionMet ( Event evnt )
        {
            if (!RunningStatus)
                return false;
            
            if ( evnt.GetType().IsSubclassOf( Type.GetType( "Core.Model.Events.IntegerDataEvent" ) ) )
            {
                return ( LinkCondition.EventData == null || 
                    LinkCondition.EventData is int && ( ( int ) LinkCondition.EventData ) == ( ( IntegerDataEvent ) evnt ).Data );
            }
            else if ( evnt.GetType().IsSubclassOf( Type.GetType( "Core.Model.Events.BooleanDataEvent" ) ) )
            {
                return ( LinkCondition.EventData == null || 
                    LinkCondition.EventData is bool && ( ( bool ) LinkCondition.EventData ) == ( ( BooleanDataEvent ) evnt ).Data );
            }
            else if ( evnt.GetType().IsSubclassOf( Type.GetType( "Core.Model.Events.OctetStringDataEvent" ) ) )
            {
                return ( LinkCondition.EventData == null ||
                    LinkCondition.EventData is OctetString && ((OctetString)LinkCondition.EventData) == ((OctetStringDataEvent)evnt).Data.Value);
            }
            else 
            {
                return true;
            }
        }


        #endregion

        public override string ToString ()
        {
            return  base.ToString () +" : "+ LinkCondition.ToString() +" Effects:"+ linkEffect.ElementaryActions.Count;
        }
        
    }
}
